<?php
    class file_model extends Model
    {
        public function __construct(){
            parent::__construct();
            $this->setTableName('files');
        }
        
        /*
        	@param string name name of file info insert
        	@param string file name of file info truly uploaded
        	@param int taskid need to append related files to
        */
        public function put($name, $file, $taskId){
        	//assert task id valid and check existense
        	$taskId = intval($taskId);
        	
			return $this->insert(array(
					'real_name' => $name,
					'store_name' => $file,
					'task_id' => $taskId
				));
        }
        
        /*
        	@param int task id that wants to get file list
        	@return array files list related to specified task id
        */
        public function get($taskId){
        	//assert valid task id
        	$taskId = intval($taskId);
        	
        	return $this->select(array('*'), array(
        			'task_id' => $taskId
        		));
        }
        
        /*
        	@param int task id needs to check
            @return bool task existence state
        */
        public function exists($id){
        	//assert id as an valid int value
        	$id = intval($id);
            
            if (empty($id)) return false;
        
        	$sql = 'select id from ' . $this->getTableName()
            	. ' where id = {0} limit 1';
            
            $info = $this->db->fetchOne($sql, array($id));
            
            return !empty($info);
        }
        
        /*
        	@param int file id of stored file
        	@return void
        */
        public function fetch($id){
        	//assert valid file id
        	$id = intval($id);
        	
        	$sql = 'select real_name, store_name from ' . $this->getTableName()
        		. ' where id = {0} limit 1';
        	
        	list($realName, $storeName) = $this->db->fetchOne($sql, array($id));
        	
        	//build file name
        	$filename = SYS_UPLOAD . '/' . $storename;
        	
        	if (!file_exists($filename)){
        		header('404 Not Found');
        		die;
        	}
        	
        	//present headers
        	$this->_sendHeader($filename, $realname);
        	
        	//send content
        	print file_get_contents($filename);
        	die;
        }
        
        /*
        	@param string fname position of the file, real path
        	@param string rname filename of the file, real name user set
        	@return void
        */
        private function _sendHeader($fname, $rname){
        	ob_clean();
            header('Content-Description: File Transfer');
            header('Content-Disposition: attachment; filename=' . $rname);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filename));
        }
    }